package com.ideal.manage.esms.controller.system;

import com.ideal.manage.esms.bean.DTO.PageDto;
import com.ideal.manage.esms.bean.DTO.Result;
import com.ideal.manage.esms.bean.project.ProjectManage;
import com.ideal.manage.esms.bean.sys.*;
import com.ideal.manage.esms.config.shiro.MyShiroRealm;
import com.ideal.manage.esms.repository.system.UserRepository;
import com.ideal.manage.esms.service.project.ProjectManageService;
import com.ideal.manage.esms.service.system.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户管理菜单
 */
@Controller
@RequestMapping("system")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @Resource
    private CustomerService customerService;
    @Resource
    private UserProjectService userProjectService;

    @Resource
    private ProjectManageService projectManageService;
    @Resource
    private ParameterService parameterService;
    @Resource
    private UserRepository userRepository;

    /**
     * 系统管理-用户管理页面
     * @param model
     */
    @RequestMapping("user_list")
    public void userList(Model model,String Q_LIKE_loginName,String Q_EQ_role) {
        List<Role> roles = roleService.findAllRoles();
        model.addAttribute("roles", roles);
        List<Customer> customers = customerService.findAllCustomer();
        model.addAttribute("customers", customers);
        model.addAttribute("Q_LIKE_loginName",Q_LIKE_loginName);
        model.addAttribute("Q_EQ_role",Q_EQ_role);
    }

    /**
     * 系统管理-用户管理-页面数据
     *
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping("user_data")
    @ResponseBody
    public PageDto userData(int pageNum, HttpServletRequest request) {
        PageDto pageDto = userService.findAllUsers(pageNum, request);
        return pageDto;
    }

    /**
     * 系统管理-用户管理-新增
     * @param model
     */
    @RequestMapping("user_add")
    public void userAdd(Model model) {
        List<Role> roles = roleService.findAllRoles();
        model.addAttribute("roles", roles);
        List<ProjectManage> projectManages=projectManageService.findAllProjects();
        model.addAttribute("projectManages",projectManages);
    }

    /**
     * 系统管理-用户管理-修改
     * @param id
     * @param model
     */
    @RequestMapping("user_edit")
    public void userEdit(String id, Model model) {
        Parameter parameter = parameterService.findAdmin("10000","admin");
        model.addAttribute("parameter",parameter);
        //获取当前用户
        MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        User  nowUser= userRepository.findOne(shiroUser.getId());
        model.addAttribute("nowUser",nowUser);

        User user = userService.findById(id);
        model.addAttribute("user", user);
        List<UserProject> userProjects=userProjectService.findProjects(user.getId());
        model.addAttribute("userProjects",userProjects);
        List<Role> roles = roleService.findAllRoles();
        model.addAttribute("roles", roles);
        List<ProjectManage> projectManages=projectManageService.findAllProjects();
        model.addAttribute("projectManages",projectManages);
    }

    /**
     * 系统管理-用户管理-保存用户
     * @param id
     * @param loginName
     * @param password
     * @param checkPassword
     * @param role
     * @param object
     * @return
     */
    @RequestMapping("user_save")
    @ResponseBody
    public Result userSave(String id, String loginName, String password,String changePassword,
                           String checkPassword, String role,
                           String object) {
        User user=userService.saveUser(id, loginName, password,changePassword, checkPassword,role);
        userProjectService.saveUserProject(user,object);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("保存成功");
        return result;
    }

    /**
     * 系统管理-用户管理-删除用户
     * @param ids
     * @return
     */
    @RequestMapping("user_del")
    @ResponseBody
    public Result userDel(String[] ids) {
        userService.delUser(ids);
        userProjectService.delUserProjects(ids);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("删除成功");
        return result;
    }
}
